import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wanAndroid/common/uts/sp_uts.dart';
import 'package:wanAndroid/common/uts/toast_uts.dart';
import 'package:wanAndroid/config/AppConfig.dart';
import 'package:wanAndroid/page/page_guangchang.dart';
import 'package:wanAndroid/page/page_me.dart';
import 'package:wanAndroid/page/page_sy.dart';
import 'package:wanAndroid/page/page_wenda.dart';

class PageHome extends StatefulWidget {
  ///启动页
  static Future<void> start(BuildContext context) async {
    return await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => PageHome()),
    );
  }

  ///启动页
  static Future<void> startAndRemove(BuildContext context) async {
    return await Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => PageHome()),
      (r) => r == null,
    );
  }

  @override
  State<StatefulWidget> createState() => _PageHome();
}

class _PageHome extends State<PageHome> {
  var _currPos = 0;

  final Pages = <PageBean>[
    PageBean(title: "首页", tag: 1, icon: Icon(Icons.home)),
    PageBean(title: "问答", tag: 2, icon: Icon(Icons.call)),
    PageBean(title: "广场", tag: 3, icon: Icon(Icons.sanitizer)),
    PageBean(title: "个人中心", tag: 4, icon: Icon(Icons.access_alarm)),
  ];

  ///页面列表
  final _pageList = <Widget>[
    PageSy(),
    PageWenDa(),
    PageGuangChange(),
    PageMe(),
  ];

  ///时间
  DateTime lastPopTime;

  @override
  void initState() {
    //初始化登录状态
    SPUts.setBoolean(AppConfig.KEY_IS_LOGIN, false);
    super.initState();
  }

  @override
  void dispose() {
    //app关掉的时候
    SPUts.setBoolean(AppConfig.KEY_IS_LOGIN, false);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => WillPopScope(
        onWillPop: () {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
            lastPopTime = DateTime.now();
            ToastUts.showToast(context, text: '再按一次退出');
          } else {
            lastPopTime = DateTime.now();

            /// 清空数据
            SPUts.clear();
            SPUts.setBoolean(AppConfig.KEY_IS_LOGIN, false);
            SystemNavigator.pop();
          }
          return;
        },
        child: Scaffold(
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(0.0),
            child: AppBar(),
          ),
          body: Stack(
            children: [
              ///首页
              Offstage(
                offstage: _currPos != 0,
                child: _pageList[0],
              ),

              ///问答
              Offstage(
                offstage: _currPos != 1,
                child: _pageList[1],
              ),

              ///广场
              Offstage(
                offstage: _currPos != 2,
                child: _pageList[2],
              ),

              ///个人中心
              Offstage(
                offstage: _currPos != 3,
                child: _pageList[3],
              ),
            ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            iconSize: 25,
            showUnselectedLabels: true,
            selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
            unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
            unselectedItemColor: Colors.grey,
            selectedItemColor: Colors.redAccent,
            currentIndex: _currPos,
            onTap: (pos) {
              if (pos == _currPos) return;
              setState(() {
                _currPos = pos;
              });
            },
            items: Pages.map((e) =>
                BottomNavigationBarItem(icon: e.icon, label: e.title)).toList(),
          ),
        ),
      );
}

/// 页面测试
class PageBean {
  final String title;
  final int tag;
  final Widget icon;

  PageBean({this.title, this.tag, this.icon});
}
